/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.moravian.grid;

import edu.moravian.entity.PathEntity;
import edu.moravian.entity.Tower;
import edu.moravian.math.Point2D;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author szilagyi
 */
public class Grid
{
    
    private int numCellsAcross;
    private int numCellsUpDown;
    
    private double cellHeight;
    private double cellWidth;
    
    private WorldDescriptor world;
    
    private List<GridCell> cells;
    
    public Grid(int numCellsAcross, int numCellsUpDown, WorldDescriptor world)
    {
        this.numCellsAcross = numCellsAcross;
        this.numCellsUpDown = numCellsUpDown;
        this.world = world;
        
        this.cellWidth = world.getWidth() / (double)numCellsAcross;
        this.cellHeight = world.getHeight() / (double)numCellsUpDown;

        this.cells = new ArrayList<GridCell>();
        
        double xCenter, yCenter;
        for (int i = 0; i < (numCellsAcross * numCellsUpDown); i++)
        {
            xCenter = (((i % numCellsAcross) * cellWidth) + (cellWidth / 2.0)) + world.getMinX();
            yCenter = (((i / numCellsAcross) * cellHeight) + (cellHeight / 2.0)) + world.getMinY();
            
            if(yCenter >= 735.0)
            {
                System.out.println("i " + i);
            }
            cells.add(new GridCell(xCenter, yCenter));
        }
    }
    
    public GridCell getCell(int index)
    {
        if (index >= cells.size())
        {
            return null;
        }
        return cells.get(index);
    }
    
    
    public double getCellWidth()
    {
        return cellWidth;
    }
    
    public double getCellHeight()
    {
        return cellHeight;
    }
    
    
    public GridCell getCell(double worldXLoc, double worldYLoc)
    {
        int cellXVal = (int)((worldXLoc - world.getMinX()) / cellWidth);
        int cellYVal = (int)((worldYLoc - world.getMinY()) / cellHeight);
        int arrayIndex = ((cellYVal * numCellsAcross) + cellXVal);
//                System.out.println("Cell is " + arrayIndex);
//                System.out.println("xloc is "+ worldXLoc + "   "+cellXVal);
//                System.out.println("yloc is "+ worldYLoc + "   "+cellYVal);
//               System.out.println("world is "+ world.getHeight());

        return getCell(arrayIndex);
    }
    
    public List<GridCell> getGrid()
    {
        return cells;
    }
    
    public void draw(World2D w2d) 
    {
        for (GridCell cell: cells)
        {
            
            if(cell.hasEntity())
            {
                if(cell.getEntity() instanceof PathEntity)
                {
                    Point2D cellCenter = cell.getCenter();

                    w2d.setColor(Color.WHITE);
                    w2d.drawCenteredCircle(cellCenter, cellWidth/15.0);
                    
                    w2d.setColor(Color.BLUE);

                    
                    Point2D lowerLeft = new Point2D(cellCenter.getX() - cellWidth / 2.0, cellCenter.getY() - cellHeight / 2.0);
                    Point2D upperRight = new Point2D(cellCenter.getX() + cellWidth / 2.0, cellCenter.getY() + cellHeight / 2.0);

                    WorldDescriptor world = new WorldDescriptor(lowerLeft, upperRight);
                    w2d.drawLine(world.getLowerLeft(), world.getLowerRight());
                    w2d.drawLine(world.getLowerLeft(), world.getUpperLeft());
                    w2d.drawLine(world.getUpperRight(), world.getLowerRight());
                    w2d.drawLine(world.getUpperRight(), world.getUpperLeft());
                }
            }
        }
          
    }
    
}
